home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / scope / 151-175 / scopedisk152 / atree / numbering_tutorial < prev    next >
Text File  |  1995-03-19  |  19KB  |  515 lines

  1.  
  2.  
  3. The Binary and Hexadecimal Numbering Systems
  4.  
  5. The binary and hexadecimal numbering systems are very important in computer
  6. science and anyone wishing to advance their knowledge of the subject to
  7. attain a deeper understanding of computers must learn them. In order to
  8. understand and use the Atree Sector Editor, an understanding of at least
  9. the hexadecimal numbering system is necessary. This tutorial is presented
  10. to assist users in fully exploiting the power of Atree and its secotr editor.
  11.  
  12. The binary and hexadecimal numbering systems are just two of an infinite
  13. number of possible numbering systems. And all numbering systems have
  14. the same properties. Once these properties are understood for numbering
  15. systems in general it becomes very easy to understand any numbering system
  16. and even design your own. This tutorial therefore is designed to bring you to
  17. an understanding of numbering systems in general first. Then our discussion of
  18. the binary and hexadecimal systems will be a snap.
  19.  
  20. We will start with what is undoubtedly familiar to everyone -- the base 10
  21. numbering system. This is the counting or numbering system we all learned
  22. in grammar school and use almost daily without thinking about it much.
  23.  
  24. In the base 10 system there are ten digits -- 1 thru 9, and 0. This is a
  25. very significant fact. In the base 5 system there are 5 digits -- 1 thru 4,
  26. and 0. In the base 8 system there are 8 digits, 1 thru 7, and 0.
  27.  
  28. The general rule is that in the base x system, there are x digits. There
  29. are 1 to x-1 digits which are related to each other by the property that
  30. each succeeding digit is equal to the former digit plus one. And there is
  31. always a digit representing the concept of nothing -- 0.
  32.  
  33. With only ten digits, how do we count or number greater than 9? Well, you
  34. all probably remember from your grammar school years that we organize
  35. digits in columns -- remember the one's column, the ten's column, the
  36. hundred's column?
  37.  
  38. A number like 728 means 8 one's, two ten's, and 7 hundred's. To obtain the
  39. value of 728, we actually multiply 8 times 1, and add it to 2 times 10, and
  40. then add this result to 7 times one hundred.
  41.  
  42. Notice that one, ten and one-hundred are all powers of ten, the base of our
  43. numbering system. In fact, that's why 10 is called the base. One is 10 to
  44. the power 0; 10 is 10 to the power 1, and one-hundred is 10 to the power 2.
  45.  
  46. Each column of a number represents another power of 10. Thus, we can have a
  47. thousand's column (10 to the third power, or ten cubed), and a
  48. ten-thousand's column (10 to the fourth power), etc.
  49.  
  50. Now think about how we count. When we get up to 9 in the one's column, we
  51. then write "10" as the next number. When we get up to 19, we then write
  52. "20" as the next number. When we get to 99 we then write "100" as the next
  53. number.
  54.  
  55. Think of the mileage indicator on your car. It starts at the factory with
  56. all columns set to 0's. Each column is represented by a small cylinder with
  57. the numbers 1,2,3,4,5,6,7,8,9,0 written evenly around the outer edge. Each
  58. cylinder does nothing but go around in circles repeatedly as you drive. But
  59. the cylinder's are interrelated so that every time a given cylinder
  60. completes one cycle and returns to "0", the cylinder immediately to the
  61. left then rolls to its next digit.
  62.  
  63. Now let's rethink what we have said so far using a base 5 system.
  64.  
  65. We will have only five digits; 1 thru 4 and 0. We will still have our one's,
  66. ten's, hundred's column, etc., but the values of these columns will not be
  67. powers of ten -- they will be powers of five, the base. The one's column will
  68. have a value of 5 to the 0 power (or 1), the ten's column will have a value
  69. of 5 to the power of 1 (or 5), the hundred's column will have a value of 5
  70. to the power of 2 (or 25), and so on.
  71.  
  72. Note that in the base 5 system, you cannot have a number like 732 -- the
  73. seven is not defined. You can have numbers like 423, 122, 444, 1000. But
  74. they won't mean the same thing as in the base 10 system.
  75.  
  76. In the base ten system, 444 means 4 hundreds, plus 4 tens, plus 4 ones. But
  77. in the base five system, 444 means 4 twenty-fives (which would be the
  78. equivalent of 100 in the base ten system), 4 fives (which would be the
  79. equivalent of 20 in the base ten system), and 4 ones (which has the same
  80. value in both systems). So, 444 in the base five system is equivalent to
  81. 124 in the base ten system.
  82.  
  83. Study closely the parallel method of calculating the value of 444 in the
  84. base 10 system on the one hand and in the base 5 system on the other:
  85.  
  86. Base 10 value of 444 =
  87.  
  88. 4 X 10 X 10 = 400
  89. 4 X 10      =  40
  90. 4 X 1       =   4
  91. TOTAL       = 444
  92.  
  93. Base 5 value of 444 =
  94.  
  95. 4 X 5 X 5 = 100
  96. 4 X 5     =  20
  97. 4 X 1     =   4
  98. TOTAL     = 124
  99.  
  100.  
  101. What would 292 (base 10) be in the base 5 system? Well, obviously what we
  102. would like to have as our end result is a base five number such that when
  103. we do the kind of conversion we did on 444 above we end up with 292 as its
  104. base ten equivalent. This number we are shooting for will have digits in it
  105. representing powers of five. Let's look at the powers of five up to the
  106. fourth power:
  107.  
  108. 5 to the 0 = 1
  109. 5 to the 1 = 5
  110. 5 to the 2 = 25
  111. 5 to the 3 = 125
  112. 5 to the 4 = 625
  113.  
  114. What we want to do is represent 292 as a sum of numbers which are multiples
  115. of powers of five. It is easy to intuitively notice in this case that 292 can
  116. be represented as:
  117.  
  118. (a)        2 X 125 + 1 X 25 + 3 X 5 + 2 X 1
  119.  
  120. This is equivalent to:
  121.  
  122. (b)        250 + 25 + 15 + 2
  123.  
  124. And this adds up to 292.
  125.  
  126. Moreover, (a) above is the key to our conversion; 125 represents the base
  127. five "thousands" column; 25 is the base five "hundreds" column, and 5 is
  128. the base five "tens" column. So the base five number we are looking for is
  129. 2132.
  130.  
  131. To double check our work, we can convert the base five number 2132 into its
  132. base ten equivalent. The 2 represents 2 times five to the third power
  133. (which is 250), and the 1 represents 1 times five to the power of 2 (which
  134. is 25), and the 3 is 3 times the power of five to the power of 1 (which is
  135. 15), and finally add in the 2; all this gives 292.
  136.  
  137. Let's try converting the base ten number 2398 into its base five
  138. equivalent.
  139.  
  140. Remember, what we want to do is represent this number as the sum of a series
  141. of multiples of powers of five.
  142.  
  143. There is a very systematic and easy to follow technique for doing this.
  144.  
  145. The first question you ask is,  "What is the largest power of five that will
  146. divide the number with a result greater than one?"
  147.  
  148. In our little table of powers of five above, we note that five to the power
  149. of two (25) divides the number with a result larger than one, but it is not
  150. the largest power of five that does so. Five to the power of three (125)
  151. also divides 2398 with a result larger than one, but so does five to the
  152. power of 4 (625). Five to the power of 5 (3125) does not divide 2398 with a
  153. result larger than one, so therefore five to the power of 4 (625) is the
  154. answer to our first question -- it is the largest power of five that
  155. divides our number, 2398, with a result that is larger than one.
  156.  
  157. The next step is to actually do the division and determine the remainder.
  158.  
  159. In this case, 2398 divided by 625 yields 3 with a remainder of 523.
  160.  
  161. We may now write 2398 as:
  162.  
  163. (a) 3 X 625 + 523  = 2398
  164.  
  165. Now, we deal with the remainder of 523 and divide it by the next smaller
  166. power of five. 625 is five to the fourth power, so the next smaller is
  167. therefore five cubed, which is 125. 523 divided by 125 yields 4 with a
  168. remainder of 23.
  169.  
  170. 523 can therefore be represented as:
  171.  
  172. (b) 4 X 125 + 23 = 523.
  173.  
  174. Substituting (b) for the 523 in (a), we get:
  175.  
  176. (c) 3 X 625 + 4 X 125 + 23 = 2398
  177.  
  178.  
  179. Now we look at the remainder of 23. And we divide it by the next smaller
  180. power of five. The next smaller power of five is now 25. 23 divided by 25
  181. yields a result of 0 with a remainder of 23. So we now re-write line (c) above as:
  182.  
  183. (d) 3 X 625 + 4 X 125 + 0 X 25 + 23 = 2398
  184.  
  185. We again look at the remainder of 23 and again we divide by the next
  186. smaller power of five, which this time is 5. 23 divided by 5 yields 4 with
  187. a remainder of three. So we can re-write (d) above as:
  188.  
  189. (e) 3 X 625 + 4 X 125 + 0 X 25 + 4 X 5 + 3 = 2398
  190.  
  191. Our remainder of 3 is now exactly divisible by the next smaller (and last)
  192. power of five (which is five to the 0 = 1).
  193.  
  194. It is now obvious that the base five equivalent of the base ten number, 2398,
  195. is 34043.
  196.  
  197. What we did to get 34043 was to simply take the digits that we needed to
  198. multiply with the decreasing powers of 5 (625, 125, 25, 5, 1) to get our
  199. base 10 number.
  200.  
  201.  
  202.  
  203. The Binary Number System
  204.  
  205.  
  206. What is called the binary number system simply has a base of 2. That means
  207. there are only two digits -- 0 and 1.
  208.  
  209. Let's take a four digit binary number and start counting -- remember the
  210. cylinder process:
  211.  
  212.  
  213.  
  214. Base 10                    Binary
  215.  
  216.  
  217. 0                                0000
  218. 1                                0001
  219. 2                                0010
  220. 3                                0011
  221. 4                                0100
  222. 5                                0101
  223. 6                                0110
  224. 7                                0111
  225. 8                                1000
  226. 9                                1001
  227. 10                            1010
  228. 11                            1011
  229. 12                            1100
  230. 13                            1101
  231. 14                            1110
  232. 15                            1111
  233.  
  234.  
  235. Since we are dealing with the base 2 system, the columns from right to left
  236. represent increasing powers of 2, starting with 2 to the power of 0 = 1, then
  237. 2 to the power of 1 = 2, then 2 to the power of 2 = 4, then 2 to the power of
  238. 3 = 8. The powers of 2 up to the 10th power are 1 ,2, 4, 8, 16, 32, 64, 128,
  239. 256, 512, 1024.
  240.  
  241. The binary representation of these powers of 2 would of course be:
  242.  
  243. --------------------------------------------------
  244. Power of two     Binary number       Decimal value
  245. --------------------------------------------------
  246.             0            1                   =            1
  247.             1            10                  =            2
  248.             2                         100              =            4
  249.             3            1000                =            8
  250.             4            10000            =            16
  251.             5            100000              =           32
  252.             6            1000000          =           64
  253.             7            10000000            =           128                                
  254.             8            100000000      =            256
  255.             9            1000000000     =           512
  256.             10           10000000000    =          1024
  257.  
  258.  
  259. Let's do some converting. What would the binary number 1011001101 be in decimal?
  260.  
  261. Let's first number the columns starting at the far right with the first column.
  262. We will start numbering with "0" so that our column numbers represent the powers
  263. of 2 we must use for the value in that column:
  264.  
  265. 9 8 7 6 5 4 3 2 1 0
  266. 1 0 1 1 0 0 1 1 0 1
  267.  
  268.  
  269. Now we can start calculating the decimal value of the binary number.
  270.  
  271. We simply calculate the decimal equivalent of the value of each of the
  272. columns of the binary number. Since the columns that have a "0" in them
  273. will always evaluate to "0", we can ignore them. The columns number of the
  274. columns which have 1's in them are:
  275.  
  276. 9
  277. 7
  278. 6
  279. 3
  280. 2
  281. 0
  282.  
  283. These represent the powers to which we have to raise the number 2 to
  284. determine the decimal value. The decimal values corresponding to these values
  285. of two are given in the small table above. We can rewrite the column numbers
  286. now with the decimal values of each colum:
  287.  
  288. 9   512
  289. 7   128
  290. 6    64
  291. 3     8
  292. 2     4
  293. 0     1
  294.  
  295. Now we add up the decimal values in the right hand column above to get
  296. 717.
  297.  
  298. What is the binary number whose decimal value is 946?
  299.  
  300. Using the table above, we see that 512 is the highest power of 2 that
  301. divides 946 with a result greater than one. This value corresponds
  302. to 2 to the power of 9. Therefore, we are dealing with a nine digit
  303. binary number -- our result must have a value (either 0 or 1) in each
  304. of the columns, 0 thru 9.
  305.  
  306. Let's write the number of these colums:
  307.  
  308. 9 8 7 6 5 4 3 2 1 0
  309.  
  310.  
  311. Now 512 divides 946 once with a remainder of 434. So we put a "1" below
  312. the 9. Column 8 represents 2 to the power of 8 which is 256. 256 divides
  313. 434 once with a remainder of 178. So se can put a "1" below it too. Column
  314. 7 represents 2 to the power of 7 which is 128. 128 divides 178 once with a
  315. remainder of 50. So we can put a "1" below it. Column 6 represents 64 and 
  316. 64 does not divide 50 with a result greater than one. So se put a "0" in
  317. column 6. Column 5 represents 32 and 32 divides 50 once with a remainder of
  318. 18. Se we can put a 1 in column 5. Column 4 represents 16. 16 divides 18 once
  319. with a remainder of 2. So we put a "1" in column 4. Columns 3 and 2 represent
  320. 8 and 4 respectively and niether of these divides 2 more than once, so we put
  321. 0's in both column 3 and column 2. Column 1 represnets 2 and 2 divides 2 once
  322. with a remainder of 0. So we put a 1 in column 1. Since the remainder is 0,
  323. we put a 0 in column 0.
  324.  
  325. We end up with 1110110010 (base 2) = 946 (base 10).
  326.  
  327. Why are binary numbers so important to the study of computer science?
  328.  
  329. The reason is that a computer's memory is made up of hundreds of thousands, or
  330. even millions, of tiny transistors. A transistor is an electronic component that
  331. has two states -- it either acts to greatly restrict the flow of current through
  332. it, or it acts to let the current flow quite freely. Hence it is spoken of as
  333. being either "on" (allowing high current flow, hence high voltage), or "off"
  334. (allowing low current flow, hence low voltage). 
  335.  
  336. Obviously, the on/off or high/low states of transistors lend themselves to
  337. binary interpretation -- 1 or 0.
  338.  
  339. You can string a number of transistors together and let each one represent
  340. a column of a binary number. In fact, that's just what computer engineers
  341. do. And the typical number of transistors so strucg together is 8. Hence,
  342. a very standard computer number is an 8 digit (or 8 column, if you wish)
  343. binary number.
  344.  
  345. An 8 digit binary number is called a byte. The digits or columns are of course
  346. numbered 0 thru 7 (instead of 1 thru 8) so that the column number represents the
  347. power of 2 that the column represents. Each column is called a bit. Thus, we speak
  348. of bit 7, or bit 6, or the zero bit.
  349.  
  350. The low bit of a binary number is always bit zero. The high bit is always the
  351. highest bit of the number. Thus, the high bit of a byte is bit 7. The low
  352. bit is also referred to as the Least Siginificant Bit, or LSB. The high
  353. bit is also referred to as the MSB; Most Siginificant Bit.
  354.  
  355. If the value of a bit is 1, the bit is said to be set. If it is 0, it is
  356. said to be clear.
  357.  
  358. In the binary number 10110, the low bit is clear, the high bit is set. The
  359. high bit is bit 4; bit 3 is clear, and bits one and two are set.
  360.  
  361. A byte can have values ranging from 00000000 to 11111111. The decimal
  362. equivalent is 000 to 255. There are a total of 256 values (0 - 255).
  363.  
  364. In general, an x bit binary number has 2 to the power of x values,
  365. ranging from 0 to (2 to the powers of x) - 1.
  366.  
  367. Thus, a byte, being an 8 bit binary number, has 2 to the power of 8
  368. (256) values, ranging from 0 to 255. A 16 bit binary number has 2 to the
  369. power of 16 (or 256 X  256 = 65,536) values, ranging from 0 to 65,535.
  370.  
  371. The first four bits of a byte are referred to as the low nibble; the last
  372. four bits are referred to as the high nibble.
  373.  
  374. When we write decimal numbers, we conventionally use a comma every three
  375. columns to make the number easier to comprehend. Spaces are typically used
  376. for this purpose with binary numbers. Sometimes spaces are used between
  377. the nibbles of bytes. Thus 10110011 is written as 1011 0011. Alternatively,
  378. a space separates bytes in longer binary numbers. A 32 bit binary number
  379. is thus frequently rendered as four bytes separated by spaces. Thus,
  380.  
  381.             10110011 01011100 10101111 00010010
  382.  
  383.  
  384.  
  385. The Hexadecimal Numbering System
  386.  
  387.  
  388. Don't be alarmed by the name -- we are simply talking about a base 16
  389. numbering system.
  390.  
  391. In the numbering systems we have looked at so far the base has always been
  392. less than 10. We therefore needed less than 10 symbols to represent our
  393. digits and we could therefore use the same ones we are familiar with from
  394. the base 10 system (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, and 0).
  395.  
  396. What do we do when we are dealing with number systems whose bases are larger
  397. than 10?
  398.  
  399. We use all of the numbers from the base 10 system, and when we run out, we
  400. use the letters of the alphabet.
  401.  
  402. Thus, the base 12 system would have 12 digits as follows:
  403.  
  404.                   0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B.
  405.  
  406. The base 14 system would have 14 digits as follows:
  407.  
  408.                   0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D.
  409.  
  410.  
  411. And the base 16 (or hexadecimal) system would have 16 digits as follows:
  412.  
  413.                                     0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F.
  414.  
  415.  
  416.  
  417.  
  418.  
  419. In all of the above, the symbols A thru F have the decimal equivalent values
  420. of 10 thru 15 respectively.
  421.  
  422. Let's convert the hexadecimal number A7C034F into its decimal equivalent.
  423.  
  424. First, let's determine the values of the columns. Our particular number
  425. has 7 columns, 0 thru 6. And we are dealing with powers of 16, the base.
  426. Let's write down the decimal values of these powers as follows:
  427.  
  428. Power of 16           Decimal Value
  429.  
  430.         0                                                1
  431.         1                                                16
  432.         2                                                256
  433.         3                                                4096
  434.         4                                                65536
  435.         5                                                1048576
  436.         6                                                16777216
  437.  
  438.  
  439. Now we can re-write this table showing the digits of the hexadecimal number
  440. in the correct places:
  441.  
  442. Power of 16           Decimal Value     hex digit
  443.  
  444.         0                                                1                                F
  445.         1                                                16                            4
  446.         2                                                256                            3
  447.         3                                                4096                        0
  448.         4                                                65536                        C
  449.         5                                                1048576                    7
  450.         6                                                16777216                A
  451.  
  452.  
  453.  
  454. We can write this table one more time. This time, we will include the 
  455. decimal values of the hex digits A thru F:
  456.  
  457. Power of 16           Decimal Value     hex digit  Dec value of hex
  458.  
  459.         0                                                1                                F                            15
  460.         1                                                16                            4                             4
  461.         2                                                256                            3                             3
  462.         3                                                4096                        0                             0
  463.         4                                                65536                        C                            12
  464.         5                                                1048576                    7                             7
  465.         6                                                16777216                A                            10
  466.  
  467.  
  468.  
  469. Now, to get the decimal value of our hex number all we have to do is
  470. multiply the values in the "Decimal Value" column by the corresponding
  471. values in the "Dec value of hex" column and add the results together.
  472.  
  473. The hexadecimal numbering system is very commonly used in computer science.
  474. Why? Because it has a very close relationship to the base 2, or binary
  475. numbering system.
  476.  
  477. This relationship becomes clear when we examine a table showing the binary
  478. equivalent of the 16 hexadecimal digits.
  479.  
  480.  
  481.                                     Hex Digit              Binary Equivalent
  482.  
  483.                                             0                                                    0000
  484.                                             1                                                    0001
  485.                                             2                                                    0010
  486.                                             3                                                    0011
  487.                                             4                                                    0100
  488.                                             5                                                    0101
  489.                                             6                                                    0110
  490.                                             7                                                    0111
  491.                                             8                                                    1000
  492.                                             9                                                    1001
  493.                                             A                                                    1010
  494.                                             B                                                    1011
  495.                                             C                                                    1100
  496.                                             D                                                    1101
  497.                                             E                                                    1110
  498.                                             F                                                    1111
  499.  
  500.  
  501.  
  502. Notice that four bits of a binary digit correspond to one hexadecimal digit.
  503. Therefore 8 bits correspond to two hex digits. A byte, in other words, can
  504. be represented as two hexadecimal digits. And to translate quickly from one
  505. to the other, all you need is the above table.
  506.  
  507. Thus, 11100011 in binary is E3 in hex. A5 is 10100101 in binary.
  508.  
  509. Learning to live comfortably with several numbering systems is a must
  510. for those interested in geting below the surface level of computer
  511. understanding. Playing with various numbering systems and playing with
  512. converting values from one to another is a good way of developing your
  513. comfort with them.
  514.  
  515.